import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { visualizer } from 'rollup-plugin-visualizer'

import createAutoImport from './auto-import'
import createSvgIcon from './svg-icon'
import createCompression from './compression'
import createSetupExtend from './setup-extend'

export default function createVitePlugins(viteEnv, isBuild = false) {
  const { VITE_BUILD_ANALYZE } = viteEnv
  const vitePlugins = [
    vue(),
    vueJsx(),
    createSetupExtend(),
    ...createAutoImport(),
    createSvgIcon(isBuild)
  ]

  if (isBuild) {
    vitePlugins.push(...createCompression(viteEnv))

    // 打包完成后自动打开浏览器，显示产物体积报告
    VITE_BUILD_ANALYZE === 'true' && vitePlugins.push(visualizer({ open: true }))
  }

  return vitePlugins
}
